#include "Clock.h"
#include "Base.h"
#include <Windows.h>
#include <assert.h>

#if defined (_DEBUG)
#include "Memory/MemLeaks.h"
#endif

//-------------------------------------------------
CClock::CClock(uint32 _uAvgSamples)
	: m_uSamples(_uAvgSamples)
	, m_fElpasedTime(0.f)
	, m_dLastTime(0)
	, m_fFPS(0.f)
	, m_fFPSTime(0.f)
	, m_uFPSCount(0)
	, m_fLastFps(0.f)
	, m_fTotalTime(0.f)
	, m_uIndex(0)
{
	m_Deltas = new float[m_uSamples];

	for(uint32 j=0; j<m_uSamples; ++j)
	{
		m_Deltas[j] = 0;
	}
}

//-------------------------------------------------
CClock::~CClock()
{
	CHECKED_DELETE_ARRAY(m_Deltas);
}

//-------------------------------------------------
void CClock::Update(float _fAddRestValue)
{
	//-----Actualizacion de los valores FPS y ElpasedTime-----
	//Calculo de la diferencia de tiempo (m_fElpasedTime)
	double	l_dCurTime = timeGetTime();

	if ( _fAddRestValue != 0.f ) 
		l_dCurTime = l_dCurTime * _fAddRestValue;

	if( m_dLastTime == 0 ) 
		m_dLastTime = l_dCurTime;

	m_Deltas[m_uIndex]	= (float)((l_dCurTime - m_dLastTime) * 0.001);
	m_dLastTime			= l_dCurTime;

	float d = 0;
	for(unsigned int j=0; j<m_uSamples; ++j)
	{
		d += m_Deltas[j];
	}
	d /= (float)m_uSamples;
	m_fElpasedTime = d;
	m_uIndex = (++m_uIndex) % m_uSamples;


	//Calculo de los frames por segundo (m_fFPS)
	++m_uFPSCount;
	m_fFPSTime += m_fElpasedTime;

	m_fFPS = m_uFPSCount/m_fFPSTime;

	if( l_dCurTime - m_fLastFps > 1000 )	//Solo mostramos los fps cada segundo
	{
		m_fFPS = m_uFPSCount/m_fFPSTime;
		m_uFPSCount = 0;
		m_fFPSTime = 0;
		m_fLastFps = (float)l_dCurTime;
	}

	m_fTotalTime += m_fElpasedTime;
}

//-------------------------------------------------
float CClock::GetElapsedTime() const
{
	return m_fElpasedTime;
}

//-------------------------------------------------
float CClock::GetFPS() const
{
	return m_fFPS;
}

//-------------------------------------------------
float CClock::GetTotalTime() const
{
	return m_fTotalTime;
}

